   var constant_version_id = $.trim($("#constant_version_id").val());
   
   //返回常量集
   var go_back = function(){
	   var url = base + "/dataCenter/constantLibrary?name=设计常量库";
	   $.get(url, function(result){
		   $("#content").html("");
		   $("#content").html(result);
	   });
   }
   
   var initData = function(num){
	    var param = {
		    	constant_version_id:constant_version_id,
				page_num:num,
				page_size:"10"
		    }
			$.ajax({
		        url: base + "/dataCenter/businessdata/findConstantInfoList",
		        data: param,
		        success: function (data, textStatus, jqXHR) {
		        	if(data != null){
			        	var pageCount = data.pager.pageCount;
			        	var dataObj ={
			        			"list":data.list,
			         			"dataTool":{
			         				"title":{
			          			      	"standard_type_jc": "数据类型(常量,代码)"
			          			      	,"code_name": "数据项代号"
			          			      	,"code_cname":"数据项中文简称"
			          			      	,"code_parent_name":"上级数据项代号"
			          			      	,"has_children":"是否包含代码集"
			          			      	,"code_value":"数据项取值"
			          			      	,"code_value_unit":"数据项取值单位"
			          			      	,"code_default_value":"数据项默认取值"
			          			      	,"code_create_user":"数据项创建人"
			          			      	,"remark":"备注"
			          			      	,"operation":"操作"
			            			},
                                    "stateData":[{"stateField":"has_children","stateArray":[{1:"是"},{0:"否"}]}],
                                    "titleCheckbox":false,
									"btn":[{"event":"updateObj","name":"编辑"},
									       {"event":"delObj","name":"删除"}]
								}
				  		};
			        	showPageTable('view', dataObj,num,pageCount,"initData");
		        	}
		        },
		        error: function () {
		        }
		    });
   }
   
   //编辑常量值
   var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"id",
					   "code_name":"数据项代号",
					   "code_cname":"数据项中文简称",
					   "code_value":"数据项取值",
					   "has_children":"是否包含代码集",
					   "code_value_unit":"数据项取值单位",
					   "code_default_value":"数据项默认取值",
					   "remark":"备注"
				   },"text":{
					   "id":obj.id,
					   "code_name":obj.code_name,
					   "code_cname":obj.code_cname,
					   "code_value":obj.code_value,
					   "has_children":"",
					   "code_value_unit":obj.code_value_unit,
					   "code_default_value":obj.code_default_value,
					   "remark":obj.remark
				   },"input":{
					   "id":"hidden",
					   "code_name":"text",
					   "code_cname":"text",
					   "code_value":"text",
					   "has_children":"switch",
					   "code_value_unit":"text",
					   "code_default_value":"text",
					   "remark":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initSwitch(obj.has_children);
   }
   //添加代码值
   var addCodeData = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "standard_type":"数据类型(常量/代码)",
					   "code_name":"数据项代号",
					   "code_cname":"数据项中文简称",
					   "code_value":"数据项取值",
					   "code_parent_name":"上级数据项取值",
					   "code_value_unit":"数据项取值单位",
					   "code_default_value":"数据项默认取值",
					   "remark":"备注"
				   },"text":{
					   "standard_type":"1",
					   "code_name":"",
					   "code_cname":"",
					   "code_value":"",
					   "code_parent_name":"",
					   "code_value_unit":"",
					   "code_default_value":"",
					   "remark":""
				   },"input":{
					   "standard_type":"hidden",
					   "code_name":"text",
					   "code_cname":"text",
					   "code_value":"text",
					   "code_parent_name":"select",
					   "code_value_unit":"text",
					   "code_default_value":"text",
					   "remark":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initParentName();//初始化上级常量值
   }
   
   //初始化上级常量值
   var initParentName = function(){
	   $.post(base+"/dataCenter/businessdata/findParentConstant",{constant_version_id:constant_version_id},
	   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var optionstring = "";
	   	                 $.each(data, function(i,item){
	   	                    optionstring += "<option value=\"" + item.code_name + "\" >" + item.code_cname + "</option>";
	   	                });
	   	                $("select[name='code_parent_name']").html('<option value=""></option>' + optionstring);
	   	      		}else{
	   	      		  	$("select[name='code_parent_name']").html('<option value=""></option>');
	   	      		}
	     		    form.render(); 
	  });
   }
   
   //添加常量值
   var addConstantInfo = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "standard_type":"数据类型(常量/代码)",
					   "code_name":"数据项代号",
					   "code_cname":"数据项中文简称",
					   "code_value":"数据项取值",
					   "has_children":"是否包含代码集",
					   "code_value_unit":"数据项取值单位",
					   "code_default_value":"数据项默认取值",
					   "remark":"备注"
				   },"text":{
					   "standard_type":"0",
					   "code_name":"",
					   "code_cname":"",
					   "code_value":"",
					   "has_children":"",
					   "code_value_unit":"",
					   "code_default_value":"",
					   "remark":""
				   },"input":{
					   "standard_type":"hidden",
					   "code_name":"text",
					   "code_cname":"text",
					   "code_value":"text",
					   "has_children":"switch",
					   "code_value_unit":"text",
					   "code_default_value":"text",
					   "remark":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
	   initSwitch();
   }
   
   
   var initSwitch = function(flag){
	   var html = '';
	   if(flag=="true"){
		   html += '<input checked="" name="has_children" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
	   }else{
		   html += '<input name="has_children" lay-skin="switch" lay-filter="switch" lay-text="是|否" type="checkbox" > ';
	   }
	   $("#switch").html(html);
	   form.render(); 
   }
   
   //初始化上级数据项
   var initParentCode = function(){
	   $.post(base+"/dataCenter/businessdata/findDesignContent",{major_id:major_id},
	   		  	function(data,status){
	     		    	if(data!=''){
	   	      		  	 var optionstring = "";
	   	                 $.each(data.list, function(i,item){
	   	                    optionstring += "<option value=\"" + item.id + "\" >" + item.project_design_content_name + "</option>";
	   	                });
	   	                $("select[name='project_design_content_id']").html('<option value=""></option>' + optionstring);
	   	      		}else{
	   	      		  	$("select[name='project_design_content_id']").html('<option value=""></option>');
	   	      		}
	     		    form.render(); 
	  });
	   
   }
   
   //数据校验
   var verifyData = function(data){
		if(data.code_name == ''){
			var obj = $("input[name='code_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.code_cname == ''){
			var obj = $("input[name='code_cname']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.code_value == ''){
			var obj = $("input[name='code_value']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.code_value_unit == ''){
			var obj = $("input[name='code_value_unit']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.code_default_value == ''){
			var obj = $("input[name='code_default_value']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.code_parent_name == ''){
			alert("上级数据项选项不能为空!");
			return false;
		}
		return true;
   }
   
   //渲染弹窗
   var layopen = function(contentDiv){
	   layer.open({
		  	title:'更新常量集'
			,content: contentDiv
	    	,area: ['700px', '700px']
	        ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
				var data = $(layero).find("form").serializeJSON();
				if(verifyData(data)){
					submitData(data);
				}
			}
	   });
   }
   
   var submitData = function(data){
		if(data.has_children == "on"){
			data.has_children = true;
		}else{
			data.has_children = false;
		}
	   var param = {
			   id:data.id,
			   constant_version_id:constant_version_id,
			   standard_type:data.standard_type,
			   code_name:data.code_name,
			   code_cname:data.code_cname,
			   code_parent_name:data.code_parent_name,
			   has_children:data.has_children,
			   code_value:data.code_value,
			   code_value_unit:data.code_value_unit,
			   code_default_value:data.code_default_value,
			   remark:data.remark
			   }
	   $.ajax({
		   url:base+"/dataCenter/businessdata/updateConstant",
		   data:param,
		   success:function(data,status){
			    if(data.statusCode == 0){
					layer.msg(data.message, {
						icon : 1
					});
			    }else{
					layer.msg(data.message, {
						icon : 5
					});
			    }
			    initData(pnum);
		   }
	   });
   }
   
   
   //删除常量集
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delConstantInfoList",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   $(document).ready(function(){
	   initData(1);
	   $("#addConstantData").click(addConstantInfo);
	   $("#addCodeData").click(addCodeData);
   });
